# -*- coding: utf-8 -*-
from django import forms
from error_messages import *
from models import *
from django.forms.util import ErrorList

class LoginForm(forms.Form):
    """Definition of LoginForm for validation

    Necessary fields are:
        email --> EmailField
        password --> 6-20 characters
    """

    email = forms.EmailField(error_messages = LOGIN_EMAIL_ERRORMESSAGES, label = u'你的Email')
    password = forms.CharField(widget = forms.PasswordInput, max_length = 20, label = u'你的MicroBlog密码',
                               min_length = 6, error_messages = PASSWORD_ERRORMESSAGES)

    def clean(self):

        email = self.cleaned_data.get('email', '')
        password = self.cleaned_data.get('password', '')
        query_set = User.objects.filter(email = email, password = password)
        if len(query_set) != 1:
            if not self._errors.get('email', ''):
                self._errors['email'] = ErrorList([EMAIL_OR_PASSWORD_ERROR])
                
            if password != '':
                del self.cleaned_data['password']
        return self.cleaned_data

class RegisterForm(forms.Form):
    """Definition of RegisterForm for validation

    Necessary fields are:
        email --> EmailField
        nick_name --> unique
        password --> 6-20 characters
        confirm_password --> same to password
    """
	
    email = forms.EmailField(error_messages = REGISTER_EMAIL_ERRORMESSAGES, label = u'你的Email')
    nick_name = forms.CharField(max_length = 30, label = u'你的MicroBlog昵称', min_length = 4, error_messages = NICK_NAME_ERRORMESSAGES)
    password = forms.CharField(widget = forms.PasswordInput, max_length = 20, label = u'你的MicroBlog密码',
                               min_length = 6, error_messages = PASSWORD_ERRORMESSAGES)
    confirm_password = forms.CharField(widget = forms.PasswordInput, max_length = 20, label = u'确认密码',
                                       min_length = 6, error_messages = PASSWORD_ERRORMESSAGES)
								
    def clean(self):
	email = self.cleaned_data.get('email', '')
	nick_name = self.cleaned_data.get('nick_name', '')
	password = self.cleaned_data.get('password', '')
	confirm_password = self.cleaned_data.get('confirm_password', '')
	email_query_set = User.objects.filter(email = email)
	nick_name_query_set = User.objects.filter(nick_name = nick_name)

	form_error = False
	
        if len(email_query_set) > 0:
            form_error = True
            if not self._errors.get('email', ''):
                self._errors['email'] = ErrorList([EMAIL_EXISTS_ERROR])
			
	if len(nick_name_query_set) > 0:
            form_error = True
            if not self._errors.get('nick_name', ''):
                self._errors['nick_name'] = ErrorList([NICKNAME_EXISTS_ERROR])

	if password != confirm_password:
            form_error = True
            if not self._errors.get('confirm_password', ''):
                self._errors['confirm_password'] = ErrorList([PASSWORD_INCOINCIDENCE_ERROR])

        if self._errors.get('password', '') or self._errors.get('confirm_password', ''):
            form_error = True

        if form_error:
            if password != '':
                del self.cleaned_data['password']
            if confirm_password != '':
                del self.cleaned_data['confirm_password']
			
	return self.cleaned_data
		
		
		

    
